#!/bin/sh
clear
#++++++++++++++++++++ 1. 先判断~/是否存在编解码器的目录++++++++++++++++++++++++++++++
if [ ! -d $HOME/编解码器 ];then echo " 温馨提示: 请先将对应的编解码器放到主目录先!";exit 0; fi
#++++++++++++++++++++ 2 获取当前系统的桌面名称和当前系统密码++++++++++++++++++++++++++
read -p "请输入当前系统密码:" SysPassword
if [ ! -d $HOME/Desktop ];then DesktopPath=$HOME"/桌面";else DesktopPath=$HOME"/Desktop";fi && clear
#++++++++++++++++++++ 3. 初始化路径和变量 ++++++++++++++++++++++++++
AutoExec=$HOME"/.config/autostart" #启动项的目录是:${AutoExec}
ApplicationsPath="/usr/share/applications"  #公共快捷方式目录是:${ApplicationsPath}
AppPath=$HOME"/编解码器"   #编解码器的根目录是:${AppPath}
AppIco=".ico-if"     #编解码器图标路径:${AppIco}
EXE="startCoder.sh"   #编解码器的启动脚本文件名:${EXE}
#==================================================================================
echo ${sys_password} | sudo -S chmod -R 777 $AppPath && clear
#=========================创建APP执行脚本(startCoder.sh)=================================
if [ -e {AppPath}/${EXE} ];then mv ${AppPath}/${EXE} ${AppPath}/bak-${EXE} && rm -rf ${AppPath}/${EXE};fi
touch ${AppPath}/${EXE} && sudo chmod 777 ${AppPath}/${EXE}
echo "#!/bin/bash">${AppPath}/${EXE}
echo 'export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH;'>>${AppPath}/${EXE}
echo "#检测网络链接畅通">>${AppPath}/${EXE}
echo "function network()">>${AppPath}/${EXE}
echo "{">>${AppPath}/${EXE}
echo "    #超时时间">>${AppPath}/${EXE}
echo "    local timeout=1">>${AppPath}/${EXE}
echo "    #目标网站(这里用服务器的IP)">>${AppPath}/${EXE}
echo '    ServerIP=$(awk {print} $(cd $(dirname $0);pwd)/config.ini | grep -w Server | grep -oP "\K([0-9]{1,3}[.]){3}[0-9]{1,3}")'>>${AppPath}/${EXE}
echo '    local target=$ServerIP'>>${AppPath}/${EXE}
echo "    #获取响应状态码">>${AppPath}/${EXE}
echo '   local ret_code=`curl -I -s --connect-timeout ${timeout} ${target} -w %{http_code} | tail -n1`'>>${AppPath}/${EXE}
echo '    echo $ret_code;'>>${AppPath}/${EXE}
echo '    if [ "x$ret_code" = "x200" ]; then'>>${AppPath}/${EXE}
echo "        #网络畅通">>${AppPath}/${EXE}
echo "        return 1">>${AppPath}/${EXE}
echo "    else">>${AppPath}/${EXE}
echo "        #网络不畅通">>${AppPath}/${EXE}
echo "        return 0">>${AppPath}/${EXE}
echo "   fi">>${AppPath}/${EXE}
echo "    return 0">>${AppPath}/${EXE}
echo "}">>${AppPath}/${EXE}
echo "while(true); do">>${AppPath}/${EXE}
echo 'echo "检测网络";'>>${AppPath}/${EXE}
echo "network">>${AppPath}/${EXE}
echo 'if [ $? -eq 1 ];then'>>${AppPath}/${EXE}
echo '	echo "网络OK！"'>>${AppPath}/${EXE}
echo "	pkill ifaudio_server">>${AppPath}/${EXE}
echo "	#pkill MeetingCoder">>${AppPath}/${EXE}
echo "	v4l2-ctl -d 0 -i 0 -DVI">>${AppPath}/${EXE}
echo "	sleep 5">>${AppPath}/${EXE}
echo "	cd ~/编解码器">>${AppPath}/${EXE}
echo "	nohup ./ifaudio_server >ifaudio.out 2>&1 &">>${AppPath}/${EXE}
echo "	./MeetingCoder >MeetingCoder.out 2>&1 &">>${AppPath}/${EXE}
echo "	unclutter -idle 0.01">>${AppPath}/${EXE}
echo "	break;">>${AppPath}/${EXE}
echo "fi">>${AppPath}/${EXE}
echo "clear">>${AppPath}/${EXE}
echo 'echo "网络未就绪,请检查  ./config.ini中的Server字段是否正确";'>>${AppPath}/${EXE}
echo "sleep 2;">>${AppPath}/${EXE}
echo "done;">>${AppPath}/${EXE}


#========================#创建:快捷方式(编解码器.desktop)===========================================
if [ -f ${ApplicationsPath}/编解码器.desktop ];then sudo rm -rf ${ApplicationsPath}/编解码器.desktop;fi 	#删除桌面的快捷方式的软链接
sudo touch $ApplicationsPath/编解码器.desktop
echo ${sys_password} |sudo -S chmod 777 ${ApplicationsPath}/编解码器.desktop
echo "[Desktop Entry]"> ${ApplicationsPath}/编解码器.desktop
echo "Name=编解码器">> ${ApplicationsPath}/编解码器.desktop
echo "Comment=无纸化同屏工具">> ${ApplicationsPath}/编解码器.desktop
echo "Exec=${AppPath}/${EXE} %u">> ${ApplicationsPath}/编解码器.desktop
echo "Icon=${AppPath}/${AppIco}">> ${ApplicationsPath}/编解码器.desktop
echo "Terminal=true">> ${ApplicationsPath}/编解码器.desktop
echo "Type=Application">> ${ApplicationsPath}/编解码器.desktop
echo "Categories=Application;Development;">> ${ApplicationsPath}/编解码器.desktop
if [ -L ${AutoExec}/编解码器.desktop ] ;then rm -rf ${AutoExec}/编解码器.desktop; fi && ln -s ${ApplicationsPath}/编解码器.desktop ${AutoExec}
if [ -L ${DesktopPath}/编解码器.desktop ] ;then rm -rf ${DesktopPath}/编解码器.desktop; fi && ln -s ${ApplicationsPath}/编解码器.desktop ${DesktopPath}

#========================#安装编解码器的依赖及环境===========================================
echo ${sys_password} | sudo -S apt update && apt install libvlc-dev libjsoncpp-dev curl libsdl2-dev libsdl2-image-dev libavcodec-dev libswscale-dev libavformat-dev libavdevice-dev v4l-utils libfaad-dev g++ vlc unclutter libfaac-dev libfaac0 -y;

#========================#安装远程工具(todesk)===================================================
if [ ! -f ${DesktopPath}/todesk_4.1.0_amd64.deb ];then wget -P ${DesktopPath} https://dl.todesk.com/linux/todesk_4.1.0_amd64.deb && sudo chmod 777 ${DesktopPath}/todesk_4.1.0_amd64.deb && sudo dpkg -i ${DesktopPath}/todesk_4.1.0_amd64.deb;fi
#========================#获取本机信息===================================================
rm -rf ${DesktopPath}/本机配置.txt
touch ${DesktopPath}/本机配置.txt
echo | lscpu >${DesktopPath}/本机配置.txt
echo "==============================================">>${DesktopPath}/本机配置.txt
echo | uname -a >>${DesktopPath}/本机配置.txt
echo "==============================================">>${DesktopPath}/本机配置.txt
echo | cat /etc/os-release >>${DesktopPath}/本机配置.txt
echo "==============================================">>${DesktopPath}/本机配置.txt
cat ${AppPath}/${EXE}
